package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.StockDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangxin
 * @description
 * @date 2020/12/3
 */
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockDao stockDao;
    @Autowired
    private GoodsDao goodsDao;

    @Transactional
    @Override
    public void saveDamageListAndGoods(User user, DamageList damageList, String damageListGoodsStr) {
        // 保存报损单
        damageList.setUserId(user.getUserId());
        stockDao.saveDamageList(damageList);
        // 保存报损单中的商品
        Integer damageListId = damageList.getDamageListId();
        List<DamageListGoods> list = JSON.parseArray(damageListGoodsStr, DamageListGoods.class);
        List<DamageListGoods> damageListGoodsList = list.stream()
                .peek(damageListGoods -> {
                    damageListGoods.setDamageListId(damageListId);
                    // 更新库存数量
                    Integer goodsId = damageListGoods.getGoodsId();
                    Goods goods = goodsDao.findGoodsById(goodsId);
                    int newCount = goods.getInventoryQuantity() - damageListGoods.getGoodsNum();
                    if (newCount >= 0) {
                        goods.setInventoryQuantity(newCount);
                        goodsDao.updateGoodsById(goods);
                    }
                })
                .collect(Collectors.toList());
        stockDao.saveDamageListGoods(damageListGoodsList);
    }

    @Transactional
    @Override
    public void saveOverflowListAndGoods(User user, OverflowList overflowList, String overflowListGoodsStr) {
        // 保存报溢单
        overflowList.setUserId(user.getUserId());
        stockDao.saveOverflowList(overflowList);
        // 保存报溢单中的商品
        Integer overflowListId = overflowList.getOverflowListId();
        List<OverflowListGoods> list = JSON.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        List<OverflowListGoods> overflowListGoodsList = list.stream()
                .peek(overflowListGoods -> {
                    overflowListGoods.setOverflowListId(overflowListId);
                    // 更新库存数量
                    Integer goodsId = overflowListGoods.getGoodsId();
                    Goods goods = goodsDao.findGoodsById(goodsId);
                    int newCount = goods.getInventoryQuantity() + overflowListGoods.getGoodsNum();
                    goods.setInventoryQuantity(newCount);
                    goodsDao.updateGoodsById(goods);
                })
                .collect(Collectors.toList());
        stockDao.saveOverflowListGoods(overflowListGoodsList);
    }

    @Override
    public List<Goods> getAllOutOfStockGoods() {
        return stockDao.getAllOutOfStockGoods();
    }

    @Override
    public List<DamageList> getDamageListByTime(String sTime, String eTime) {
        return stockDao.getDamageListByTime(sTime, eTime);
    }

    @Override
    public List<DamageListGoods> getDamageListGoodsById(Integer damageListId) {
        return stockDao.getDamageListGoodsById(damageListId);
    }

    @Override
    public List<OverflowList> getOverflowListByTime(String sTime, String eTime) {
        return stockDao.getOverflowListByTime(sTime, eTime);
    }

    @Override
    public List<OverflowListGoods> getOverflowListGoodsById(Integer overflowListId) {
        return stockDao.getOverflowListGoodsById(overflowListId);
    }
}
